package com.example.taobaou.ui.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.taobaou.R;
import com.example.taobaou.base.BaseActivity;
import com.example.taobaou.model.domain.TicketResult;
import com.example.taobaou.presenter.impl.TicketPresenterImpl;
import com.example.taobaou.utils.LogUtils;
import com.example.taobaou.utils.PresenterManager;
import com.example.taobaou.utils.TostUtils;
import com.example.taobaou.utils.UrlUtils;
import com.example.taobaou.view.ITicketPagerCallback;

public class TicketActivity extends BaseActivity implements ITicketPagerCallback {


    private TicketPresenterImpl ticketPresenter;
    private ImageView mTicketBack;//返回按钮
    private ImageView mTicketcover;//图片
    private TextView mTicketCopyOrOpen;//复制并打开淘宝
    private EditText mTocketCode;//淘口令
    private ProgressBar loadingView;

    private TextView retryLoadingView;
    private Boolean mHasTaobao = false;

    @Override
    protected int getResId() {
        return R.layout.activity_ticket;
    }

    @Override
    protected void initView() {
        mTicketBack = findViewById(R.id.img_ticket_back);
        mTicketcover = findViewById(R.id.img_ticket_cover);
        mTicketCopyOrOpen = findViewById(R.id.btn_ticket_copy_or_open);
        mTocketCode = findViewById(R.id.tex_ticket_code);
        retryLoadingView = findViewById(R.id.retry_ticket);
        loadingView = findViewById(R.id.loading_ticket);
    }

    @Override
    protected void initEvent() {
        mTicketBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mTicketCopyOrOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //复制淘口令到粘贴板
                String ticketCode = mTocketCode.getText().toString().trim();
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clipData = ClipData.newPlainText("taobao_ticket_code", ticketCode);
                cm.setPrimaryClip(clipData);
                //判断是否安装淘宝
                if (mHasTaobao) {
                    //如果安装淘宝
                    TostUtils.showToast("已复制淘口令，正在跳转淘宝.......");
                    //打开淘宝
                    Intent taobaoIntent = new Intent();
                    ComponentName componentName = new ComponentName("com.taobao.taobao","com.taobao.tao.TBMainActivity");
                    taobaoIntent.setComponent(componentName);
                    startActivity(taobaoIntent);
                }else {
                    //如果未安装
                    TostUtils.showToast("已复制淘口令，未检测到淘宝应用，请自行打开");
                }

            }
        });
    }

    @Override
    protected void initPresenter() {
        ticketPresenter = PresenterManager.getInstance().getTicketPresenter();
        if (ticketPresenter != null) {
            ticketPresenter.registerViewCallback(this);
        }

        PackageManager packageManager = getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo("com.taobao.taobao", PackageManager.MATCH_UNINSTALLED_PACKAGES);
            mHasTaobao = packageInfo != null;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            mHasTaobao = false;
        }
    }


    @Override
    public void onTicketLoaded(String cover, TicketResult result) {
        if (mTicketcover != null && !cover.isEmpty()) {
            String coverPath = UrlUtils.getTicket(cover);
           // LogUtils.d(this, "coverPath--------->" + coverPath);
            Glide.with(this).load(coverPath).into(mTicketcover);
        }
        String mode = result.getData().getTbk_tpwd_create_response().getData().getModel();
       // LogUtils.d(this, "mode--------->" + mode);
        if (result != null && mode != null) {
            mTocketCode.setText(mode);
        }
        if (loadingView != null) {
            loadingView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onNatworkError() {
        if (loadingView != null) {
            loadingView.setVisibility(View.GONE);
        }
        if (retryLoadingView != null) {
            retryLoadingView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onLoading() {
        if (retryLoadingView != null) {
            retryLoadingView.setVisibility(View.GONE);
        }
        if (loadingView != null) {
            loadingView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onEmpty() {

    }

    @Override
    protected void release() {
        if (ticketPresenter != null) {
            ticketPresenter.unregisiterViewCallback(this);
        }
    }
}
